package com.shanghai.shanghaielectric.UI.DilaogView;


import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.shanghai.shanghaielectric.MyApplication;
import com.shanghai.shanghaielectric.R;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import shanghaielectric_lib.Utils.DateUtil;


/**
 * Created by Administrator on 2017/9/2 0002.
 */

public class Dialog_datePickUtil extends DialogFragment {
    String date;
    @BindView(R.id.tv_title_datePick)
    TextView tvTitleDatePick;
    @BindView(R.id.dp_datepick)
    DatePicker dpDatepick;
    @BindView(R.id.bt_certain_datePick)
    Button btCertainDatePick;
    Unbinder unbinder;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }


    @OnClick(R.id.bt_certain_datePick)
    public void onViewClicked() {
    }

    public interface Diolg_datePickListener {
        void onClink(String date);
    }

    private static Diolg_datePickListener mDiolg_datePickListener;

    public static void setDiolgAlertListener(Diolg_datePickListener diolgDatePickListener) {
        mDiolg_datePickListener = diolgDatePickListener;
    }


    @OnClick({R.id.bt_certain_datePick})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_certain_datePick:
                mDiolg_datePickListener.onClink(date);
                dismiss();
                break;
        }
    }

    public Context mContext;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.m_dialog_datepick_y_m_d, container);

//        Toast.makeText(mContext, this.getTag().toString(), Toast.LENGTH_SHORT).show();
        date = mType.equals(Dialog_datePickUtil.yyyy_MM)?DateUtil.getSystemTime_yyyy_MM_dd().substring(0,7):DateUtil.getSystemTime_yyyy_MM_dd();
        unbinder = ButterKnife.bind(this, view);

        initView();
        initData();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initView() {

        tvTitleDatePick.setText(commandName);

        hideDatePickView(dpDatepick,mType);
            dpDatepick.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                @Override
                public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
                    if (mType.equals(Dialog_datePickUtil.yyyy_MM)){
                        date = i + "-" + String.format("%02d", i1 + 1) ;
                    }else {
                        date = i + "-" + String.format("%02d", i1 + 1) + "-" + String.format("%02d", i2);

                    }

                }
            });

    }

    public String commandName=" ";
    public String mType=" ";
    public static final String yyyy_MM = "YEAR_MONTH";
    public static final String yyyy_MM_dd = "YEAR_MONTH_DAY";
    public void setCommandName(String name, String type) {
        this.commandName = name;
        this.mType = type;
    }

    private void initData() {
        mContext = MyApplication.getContext();
    }

    private void hideDatePickView(DatePicker datePicker, String type) {
        if (datePicker != null) {
            Field f[] = datePicker.getClass().getDeclaredFields();
            switch (type) {
                case Dialog_datePickUtil.yyyy_MM:
                    //隐藏日
                    for (Field field : f) {
                        if (field.getName().equals("mDayPicker")
                                || field.getName().equals("mDaySpinner")) {
                            field.setAccessible(true);
                            Object dayPicker = new Object();
                            try {
                                dayPicker = field.get(datePicker);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                            ((View) dayPicker).setVisibility(View.GONE);
                        }
                    }
                    break;
                case Dialog_datePickUtil.yyyy_MM_dd:

                    break;
                default:
                    break;
            }
        }
    }


}
